import requests
import pickle
import json
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor
from multiprocessing import Process


page_url = 'https://ssr1.scrape.center/page/{}'
index_url = 'https://ssr1.scrape.center'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
}


def get_html(url):
    response = requests.get(url, headers=headers)
    return BeautifulSoup(response.text, 'lxml')


def parse_html(index):
    soup = get_html(page_url.format(index))
    divs = soup.find('div', class_='el-col el-col-18 el-col-offset-3').find_all('div', class_='el-card item m-t is-hover-shadow')
    for div in divs:
        # info
        infos = list(div.stripped_strings)
        # 标题
        # title = div.find('a', class_='name').find('h2').string
        title = infos[0]
        # 详情url
        detail_url = index_url + div.a['href']
        # 剧情简介
        detail_soup = get_html(detail_url)
        profile = list(detail_soup.find('div', class_='drama').p.stripped_strings)[0]
        # 封面
        cover_img = div.a.find('img')['src']
        # categories 分类
        # categories = [item.string for item in div.find('div', class_='categories').find_all('span')]
        category = infos[1: -5]
        # 上映信息
        release_info = ' '.join(infos[-5: -1])
        # 评分
        score = infos[-1]
        result.append({
            'title': title,
            'detail_url': detail_url,
            'profile': profile,
            'cover_img': cover_img,
            'category': category,
            'release_info': release_info,
            'score': score
        })


result = []
for i in range(1, 11):
    parse_html(i)

with open('movies.json', 'w', encoding='utf-8') as f:
    json.dump(result, f, ensure_ascii=False, indent=4)

with open('movies.pickle', 'wb') as f:
    pickle.dump(result, f)
with open('movies.pickle', 'rb') as f:
    result = pickle.load(f)
    print(result)
